package com.classic.okhttp.base.callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;

/**
 * Created by zhy on 15/12/14.
 */
public abstract class StringCallback extends Callback {

    private static final String KEY_CODE = "code";

    @Override
    public String onResult(Response response) throws IOException {
        return response.body().string();
    }

    @Override
    public void onResponse(Object response) {
        try {
            if (new JSONObject(response.toString()).getInt(KEY_CODE) == 200) {
                this.onSuccess(response.toString());
            } else {
                this.onError(new JSONObject(response.toString()).getInt(KEY_CODE));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public abstract void onSuccess(String response);

    public abstract void onError(int code);
}
